home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: design problem
- Date: Tue, 20 Feb 1996 19:40:03 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.00000049.00372a88@fred>
- References: <DMMwLp.Awv@emr1.emr.ca>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: pm9-007.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <DMMwLp.Awv@emr1.emr.ca>, jagrant@emr1.emr.ca (John Grant) wrote:
- >
- > Problem
- > -------
- > I have several file types: A,B,C...
- > Each one has an Open(), Close(), Draw() & Size() function, the nature
- > of which is specific to the file type, but which is common to all file types.
- > Each file type has specific functions as well, i.e. GetAStuff() for 'A' files.
- >
- > Implementation
- > --------------
- > I want to create a class I can use for each file, regardless of type. Later,
- > I will want to put a bunch of these into a container, i.e. an array of some
- > sort:
- > cMYFILE *f1 = new cMYFILE("xxx.a"); //file type 'A'
- > cMYFILE *f2 = new cMYFILE("yyy.b"); //file type 'B'
- > ...
- >
- > I need to call the 'generic' functions which are common to all file types:
- > Open(), Draw(), Size()
- >
- > However, I also need to be able to call functions which are specific to the
- > file type. Presumably, I must first inspect the object to determine the
- > file type:
- > if(f1->Type()=='a') f1->GetAStuff();
- > if(f1->Type()=='b') f1->GetBStuff();
- >
- > I haven't got a clue how to design these classes or how many I need. I started
- > out with a base class:
- > class cMYFILE{
- > public: cMYFILE(char *name);
- > ~cMYFILE();
- > virtual int Open(void) = 0;
- > virtual int Draw(void) = 0;
- > virtual int Size(void) = 0;
- > virtual char Type(void) = 0;
- > };
- >
- > and then decided I needed to derive an A,B,C class from cMYFILE in order
- > to implement the generic virtual functions and add in other file-specific
- > functions :
- > class cMYFILE_A:public cMYFILE{
- > public: int Open(void);
- > int Draw(void);
- > int Size(void);
- > int Type(void);
- > int GetAStuff(void);
- > };
- >
- > class cMYFILE_B:public cMYFILE{
- > public: int Open(void);
- > int Draw(void);
- > int Size(void);
- > int Type(void);
- > int GetBStuff(void);
- > };
- >
- > Then I got confused. I don't know which functions should be virtual,
- > which functions should be pure virtual and what stuff should be in the
- > base class and what stuff should be in the derived classes. I'm just
- > confused.
-
- You want to access AStuff and BStuff from the base, so the base must be aware
- that AStuff and BStuff exists. Your classes should be:
-
- class cMYFILE
- {
- public:
- cMYFILE(const char *name);
- virtual ~cMYFILE(); /*1*/ /*2*/
- virtual int Open()=0;
- virtual int Draw()=0;
- virtual int Size() const=0; /*3*/
- virtual char Type() const=0;
- virtual GetAStuff() const { return NOT_IMPLEMENTED; } /*4*/
- virtual GetBStuff() const { return NOT_IMPLEMENTED; }
- };
-
- class cMYFILE_A:public cMYFILE
- {
- public:
- cMYFILE_A(const char *name);
- int Open();
- int Draw();
- int Size() const;
- int Type() const;
- int GetAStuff() const;
- };
-
- class cMYFILE_B:public cMYFILE
- {
- public:
- cMYFILE_B(const char *name);
- int Open();
- int Draw();
- int Size() const;
- int Type() const;
- int GetBStuff() const;
- };
-
- Notes:
- (1) You'll probably need a virtual destructor: containers are used to free
- objects store in them, so delete from base pointer to derived objects.
- (2) func(void) prototype is obsolete in C++, use func().
- (3) It's best to use const whenever possible. (May be I haven't place them at
- the right place)
- (4) These are not pure virtual. That's why I do not have to define them in
- derived classes that don't implement the specific stuff.
-
- >
- > A related problem, which I haven't dealt with is to create a container
- > class which will hold a list of these files, so I can do:
- > nfile=filelist->nfile();
- > for(i=0;i<nfile;i++){
- > filelist->Open(i);
- > filelist->Draw(i);
- > filelist->Close(i);
- > }
- >
-
- Containers for polymorphic classes just stores pointers to the base class and
- manipulate the objects through these pointers. A common feature (but not
- mandatory) of objects stored in containers is the ability to copy (or clone)
- themselves. That is achieved by a virtual member function that must be redefined
- for every sub-class:
-
- class Object : public Base
- {
- public:
- Object(const Object &); // copy constructor
- virtual Object *clone() const { return new Object(*this); }
- };
-
- I hope that'll help you clarify OO programming in C++.
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-